package com.songcha.library_common.ui.view.loading

import android.animation.ValueAnimator
import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View
import android.view.animation.LinearInterpolator
import com.songcha.library_common.R
import com.songcha.library_common.util.dp2px



class FlowerLoadingView: View {
    companion object{
        private var DEFAULT_SIZE=32.dp2px()
        private var PETAL_COUNT=12
    }

    private var mColor: Int = Color.WHITE
    private lateinit var mPaint:Paint
    private var mRectF=RectF()
    private var mRadius=0f
    private var mProgress=0
    private val ANIMATION_DURATION=1200L
    private var mAnimator:ValueAnimator?=null

    constructor(context: Context) : super(context) {
        initView(context,null)
    }

    constructor(context: Context, attrs: AttributeSet):this(context,attrs,0){

    }

    constructor(context: Context, attrs: AttributeSet, defStyle:Int):this(context,attrs,defStyle,0){

    }

    constructor(context: Context, attrs: AttributeSet, defStyle:Int, defRes:Int):super(context,attrs,defStyle,defRes){
        initView(context,attrs)
    }

    private fun initView(context: Context, attrs: AttributeSet?){
        if(attrs!=null) {
            val ta=context.obtainStyledAttributes(attrs, R.styleable.FlowerLoadingView)
            mColor=ta.getColor(R.styleable.FlowerLoadingView_flower_color,Color.WHITE)
            ta.recycle()
        }

        mPaint=Paint()
        mPaint.isAntiAlias=true
        mPaint.color=mColor

    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        val widthMode=MeasureSpec.getMode(widthMeasureSpec)
        val heightMode=MeasureSpec.getMode(widthMeasureSpec)

        if(widthMode!=MeasureSpec.EXACTLY || heightMode!=MeasureSpec.EXACTLY){
            setMeasuredDimension(DEFAULT_SIZE,DEFAULT_SIZE)
        }

    }

    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        super.onLayout(changed, left, top, right, bottom)

        val petalWidth=(measuredWidth/PETAL_COUNT).toFloat()
        val petalHeight=measuredHeight/3.5

        val left=((measuredWidth-petalWidth)/2)
        val top=0f
        val right=left+petalWidth
        val bottom=petalHeight.toFloat()
        mRadius=(petalWidth/2)

        mRectF=RectF(left,top,right,bottom)


    }

    private fun startAnimation(){
        mAnimator=ValueAnimator.ofInt(0,PETAL_COUNT-1)
        mAnimator!!.repeatCount=-1
        mAnimator!!.duration=ANIMATION_DURATION
        mAnimator!!.interpolator=LinearInterpolator()
        mAnimator!!.addUpdateListener {
            mProgress=it.animatedValue as Int
            invalidate()
        }
        mAnimator!!.start()
    }



    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        if(canvas==null) return

        if(mProgress>0)
            canvas.rotate((360/PETAL_COUNT).toFloat()*mProgress,(width/2).toFloat(),(height/2).toFloat())

        for(i in 0 until PETAL_COUNT){
            mPaint.alpha=255-i*PETAL_COUNT

            canvas.drawRoundRect(mRectF,mRadius,mRadius,mPaint)

            canvas.rotate((360/PETAL_COUNT).toFloat(),(width/2).toFloat(),(height/2).toFloat())
        }


    }

    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        startAnimation()
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        mAnimator?.cancel()
    }


}